<?php

namespace app\index\controller\lawsuit;

use app\common\model\LawsuitOptional;
use app\index\dao\LawsuitOptionalDao;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Step2 控制器
 * Class Step1
 * @package app\index\controller\lawsuit;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\index\validate\lawsuit\LawCase
 */
class Step2
{
    // 请求参数
    private Request $request;
    private LawsuitOptionalDao $optionalDao;


    public function __construct(Request $request, LawsuitOptionalDao $optionalDao)
    {
        $this->request = $request;
        $this->optionalDao = $optionalDao;
    }

    /**
     * 读取
     * @return ApiResponse
     * @url /lawsuit/step1/read
     */
    public function read(): ApiResponse
    {
        $id = $this->request->param('id');

        $record = \app\common\model\Lawsuit::where('id', $id)->find();

        if(!$record){
            throw new ValidateException('找不到该案件');
        }

        $ask = $record->ask;
        $categoryId = $record->categoryId;

        if (!is_null($record->details) && !empty($record->details)) {

            $optional = $record->details;
        } else {

            $optional = $this->optionalDao
                ->select($categoryId)
                ->each(function (LawsuitOptional $optional) {
                    $optional['result'] = null;
                });
        }

        $data = [
            'id' => $id,
            'ask' => $ask,
            'details' => $optional,
        ];

        return ApiResponse::success($data);
    }

}
